/**
 * Created by tedi on 2018/4/3.
 * 电梯综合信息看板
 */

odoo.define('lift_base.lift_info', function (require) {
    "use strict";
    var ajax = require('web.ajax');
    var core = require('web.core');
    var Widget = require('web.Widget');
    var Model = require('web.Model');
    var session = require('web.session');
    var framework = require('web.framework');
    var form_common = require('web.form_common')
    var _t = core._t;

    /*每一行的时间轴*/
    var TimelineRowWidget = Widget.extend({
        template:"tyibs_lift_info_timeline_row_template",
        events:{
            "click a.tyibs-timelie-detail": "show_record_form",
        },
        init:function(parent, record){
            this._super(parent);
            this.record = record;
        },
        show_record_form:function(){
            var self = this;
            self.do_action({
                name: self.name,
                type: 'ir.actions.act_window',
                res_model: self.record.res_model,
                res_id: self.record.res_id * 1,
                views: [[false, 'form']],
                target: 'current',
                context: {},
            });
            return false;
        }
    });

    /*时间轴的底部，显示分类和过滤功能*/
    var TimelineFooterWidget = Widget.extend({
        template:"tyibs_lift_info_timeline_footer_template",
        events:{
          'click a':function(e){
              //var domain = [['id','in',this.comment_ids]];
              var category_id =  parseInt($(e.currentTarget).attr("data-id"));
              //if(category_id!=0){
              //   domain.push(["category_id","=",category_id]);
              //}
              this.trigger("change_filter_domain",category_id);
          }
        },
        init:function(parent, records){
            this._super(parent);
            this.records = records;
            //this.comment_ids = comment_ids;
        },
    });

    /*时间轴的结束点*/
    var TimelineEndRowWidget = Widget.extend({
        template:"tyibs_lift_info_timeline_end_row_template",
        init:function(parent){
            this._super(parent);
        }
    });

    /*时间轴控件*/
    var TimeLineWidget = form_common.AbstractField.extend({
        "template":"tyibs_lift_info_timeline_template",
        init:function(field_manager, node){
            this._super(field_manager, node);
            this.model = new Model("tyibs.base.lift.comment");
            this.category_model = new Model("tyibs.base.lift.comment.category");
        },
        start:function(){
            this._super();
            this.generate_category();
        },
        render_value: function() {
            console.log("render value ");
            this.generate_comment(0);
        },
        generate_comment:function(category_id){
            var domain = [['id','in',this.get_value()]];
            if(category_id!=undefined && category_id!=0){
                domain.push(['category_id','=', category_id]);
            }
            //console.log(this.get_value());
            var self = this;
            self.$("ul.timeline").empty();
            this.alive(this.model
                .query([])
                .filter(domain)
                .order_by("-operator_time")
                .all()
                .then(function(records){
                    records.forEach(function(item){
                       var time_line_row_widget = new TimelineRowWidget(self,item);
                       time_line_row_widget.appendTo(self.$("ul.timeline"));
                    });
                })).then(function(){
                    var widget = new TimelineEndRowWidget(parent);
                    widget.appendTo(self.$("ul.timeline"));
                });
        },
        generate_category:function(){
            var self = this;
            this.alive(this.category_model
                .query()
                .all()
                .then(function(records){
                    records.unshift({"name":"全部","id":"0","css_class":"btn-primary fa-bars"});
                    var footer_widget = new TimelineFooterWidget(self, records);
                    footer_widget.on("change_filter_domain", self, self.generate_comment);
                    footer_widget.appendTo(self.$("div.panel-footer"));
                }));
        }
    });

    core.form_widget_registry.add("lift_comment_timeline_widget",TimeLineWidget);

    //core.form_tag_registry.add('lift_complex_info_timeline_widget', TimeLineWidget);
    return {
        TimeLineWidget:TimeLineWidget
    }
});
